<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Infrastructure\Context\Relevance;

use SplObjectStorage;
use Slipstream\Configuration\ContextInterface;

/**
 * Base implementation of AnalyzerServiceInterface
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class AnalyzerService implements AnalyzerServiceInterface
{
	/**
	 * @var SplObjectStorage<AnalyzerInterface>
	 */
	protected $analyzers;
	
	/**
	 * Class constructor
	 * @return void
	 */
	public function __construct()
	{
		$this->analyzers = new SplObjectStorage();
	}
	
	/**
	 * Returns the relevance weight of a given context in terms of the target
	 * context
	 * 
	 * @param ContextInterface $context
	 * @param ContextInterface $targetContext
	 * @return int
	 */
	public function getWeight(ContextInterface $context, ContextInterface $targetContext)
	{
		foreach($this->analyzers as $analyzer) {
			$weight = $analyzer->getWeight($context, $targetContext);
			if($weight !== false) {
				return $weight;
			}
		}
		return 0;
	}
	
	/**
	 * Registers an analyzer with this service
	 * 
	 * @param AnalyzerInterface $analyzer
	 * @return void
	 */
	public function register(AnalyzerInterface $analyzer)
	{
		$this->analyzers->attach($analyzer);
	}
	
	/**
	 * Unregisters an analyzer from this service
	 * 
	 * @param AnalyzerInterface $analyzer
	 * @return void
	 */
	public function unregister(AnalyzerInterface $analyzer)
	{
		$this->analyzers->detach($analyzer);
	}
}